package com.tiantianyuegou.database;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.tiantianyuegou.app.YiLaGuanApp;

/**
 * 
 *@Title:数据库管理类
 *@Description:
 *@Author:trouble
 *@Since:2015-9-5
 *@Version:1.1.0
 */
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
	//tag
//	private static final String TAG = LogUtils.makeLogTag(DatabaseHelper.class);
	private static final String TAG ="DatabaseHelper";
	//数据库名称
	private static final String DB_NAME = "yilaguan";
	//数据库版本号
	private static final int DB_VERSION = 110;

	private static DatabaseHelper instance;

	private DatabaseHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	public static DatabaseHelper getInstance() {
		if (instance == null) {
			synchronized (DatabaseHelper.class) {
				if (instance == null)
					instance = new DatabaseHelper(YiLaGuanApp.getInstance());
			}
		}
		return instance;
	}

	@Override
	public void onCreate(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource) {
		try {
			TableUtils.createTable(connectionSource, SearchHistory.class);
//			TableUtils.createTable(connectionSource, UserInfo.class);
//			TableUtils.createTable(connectionSource, CommonWord.class);
//			TableUtils.createTable(connectionSource, CityInfo.class);
//			TableUtils.createTable(connectionSource, SystemConstant.class);
		} catch (SQLException e) {
			e.printStackTrace();
			Log.e(TAG, "创建表结构失败", e);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int oldVersion,
			int newVersion) {
		try {
			TableUtils.dropTable(connectionSource, SearchHistory.class, true);
//			TableUtils.dropTable(connectionSource, UserInfo.class, true);
//			TableUtils.dropTable(connectionSource, CommonWord.class, true);
//			TableUtils.dropTable(connectionSource, CityInfo.class, true);
//			TableUtils.dropTable(connectionSource, SystemConstant.class, true);
			onCreate(sqLiteDatabase, connectionSource);
		} catch (Exception e) {
			e.printStackTrace();
			Log.e(TAG, "重新创建表结构失败", e);
		}
	}

}
